#ifndef ENTRANCEMODEL_HPP
#define ENTRANCEMODEL_HPP

#include <QObject>
#include <QDateTime>
#include <QDebug>
#include <QPointer>
#include "store/Huben.hpp"
#include "store/struct/Basic.hpp"

class EntranceModel : public QObject {
    Q_OBJECT

public:
    explicit EntranceModel(QObject *parent = nullptr)
        : QObject(parent), chartType(ChartType::Rhythm),
          startTime(QDateTime::currentDateTime()),
          endTime(QDateTime::currentDateTime()),
          dates(getTodayAndYesterdaySafely()) {

        QPointer<EntranceModel> weakPtr(this);

        // 注册 Basic 状态变化的处理函数
        Huben::instance().registerStateChangedHandler(ActionWrapper::StateKey::Basic, [weakPtr](const QVariant &value) {
            if (!weakPtr) return;

            if (value.canConvert<Basic>()) {
                Basic basic = value.value<Basic>();
                if (!basic.startTime.isValid() || !basic.endTime.isValid()) {
                    qWarning() << "Basic contains invalid QDateTime objects";
                    return;
                }

                weakPtr->setStartTime(basic.startTime);
                weakPtr->setEndTime(basic.endTime);

            } else {
                qWarning() << "Received QVariant cannot be converted to Basic";
            }

        });

        Huben::instance().dispatch(ActionWrapper::ActionType::InitBasic, QVariant());

    }

    enum ChartType { Rhythm, TimeSequence };
    Q_ENUM(ChartType)

    QStringList getDates() const {
        return dates;
    }

    void setDates(const QStringList &newDates) {
        if (dates != newDates) {
            dates = newDates;  // 将新的日期列表赋值给成员变量 dates
            emit datesChanged(dates);
        }
    }

    ChartType getChartType() const {
        return chartType;
    }

    void setChartType(ChartType type) {
        if (chartType != type) {
            chartType = type;
            emit chartTypeChanged(type);
        }
    }

    QDateTime getStartTime() const {
        return startTime;
    }

    void setStartTime(const QDateTime &newStartTime) {
        if (startTime != newStartTime) {
            startTime = newStartTime;
            emit startTimeChanged(newStartTime);
        }
    }

    QDateTime getEndTime() const {
        return endTime;
    }

    void setEndTime(const QDateTime &newEndTime) {
        if (endTime != newEndTime) {
            endTime = newEndTime;
            emit endTimeChanged(newEndTime);
        }
    }

    // 添加一个函数用于根据选择的日期更新 startTime 和 endTime
    void updateTimesForSelectedDate(const QString &selectedDate) {
        QDate date = QDate::fromString(selectedDate, "yyyy-MM-dd");
        if (date.isValid()) {
            // 这里假设 startTime 设置为当天的 00:00:00，endTime 设置为当天的 23:59:59
            QDateTime newStartTime(date, QTime(0, 0, 0));
            QDateTime newEndTime(date, QTime(23, 59, 59));

            setStartTime(newStartTime);
            setEndTime(newEndTime);
        } else {
            qWarning() << "Selected date is invalid!";
        }
    }

signals:
    void chartTypeChanged(ChartType type);
    void startTimeChanged(const QDateTime &startTime);
    void endTimeChanged(const QDateTime &endTime);
    void datesChanged(const QStringList &dates);

private:
    ChartType chartType;
    QDateTime startTime;
    QDateTime endTime;
    QStringList dates;  // 存储日期数据

    static QStringList getTodayAndYesterdaySafely() {
        QDate today = QDate::currentDate();
        QDate yesterday = today.addDays(-1);

        if (!today.isValid() || !yesterday.isValid()) {
            qWarning() << "Invalid dates encountered!";
            // 返回一个包含两个空字符串的 QStringList
            return QStringList() << QString() << QString();
        }

        QString todayStr = today.toString("yyyy-MM-dd");
        QString yesterdayStr = yesterday.toString("yyyy-MM-dd");

        // 返回包含今天和昨天日期字符串的 QStringList
        return QStringList() << todayStr << yesterdayStr;
    }
};

#endif // ENTRANCEMODEL_HPP
